home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / prog_d / justone.zip / JUSTONE.PAS < prev    next >
Pascal/Delphi Source File  |  1996-01-15  |  6KB  |  197 lines

  1. {
  2. JustOne v1.1 - A Delphi Component
  3. By: Steven L. Keyser
  4.     email: 71214,3117@compuserve.com
  5.  
  6. JustOne v1.1 - Added the ABOUT property
  7.  (1/14/96)     - Eliminated the AllowMultInst property
  8.              - Eliminated the EXECUTE    property
  9.              - Added JUSTONE.HLP
  10.              - Added JUSTONE.KWF
  11.  
  12.              Notes: The ABOUT property was added simply as a learning
  13.              exercise.  The EXECUTE property was removed due to an
  14.              improvement in the component's design.  With special
  15.              thanks to Russ Chinoy, the JustOne component no
  16.              longer requires any code to be added to the user's
  17.              application.  Dropping the JustOne component onto the startup
  18.              form is all that is required now to make JustOne work for you.
  19.  
  20. JustOne v1.0 - The basic stuff.
  21.  (Oct '95)
  22.  
  23.     Purpose:    JustOne is a component which makes it easy to limit the
  24.     number of your application's instances to just one.  If a second
  25.     instance of your application starts, the first instance is brought
  26.     to the front and given the focus (or restored if it was minimized
  27.     to an icon).  The second instance then halts.
  28.  
  29.     Credit where credit is due...
  30.  
  31.     Some of the source code for this component came from a Help file
  32.     I downloaded from the Delphi Forum on CompuServe (LDELPHI.ZIP).  This
  33.     Help file, called Lloyd's Delphi Notes (Lloyd Linklater), lists many
  34.     tips on using Delphi.  One of the items addressed is how to add code
  35.     to your application which will allow just one instance to run.  In the
  36.     Help file, that code is further credited to Pat Ritchey.
  37.  
  38.     I took that snippet of code and put it into an easily re-usable
  39.     component.
  40.  
  41.     Additional ideas came from Russ Chinoy (RC Software) on a way to
  42.     have JustOne perform its function without the user having to put
  43.     any code into their application.
  44.  
  45.     JustOne is released as Freeware.  If you use it, you do so at your
  46.     own risk.  Feel free to modify this source code to suit your own
  47.     purposes.  If you enhance JustOne, I'd like to see your work.
  48. }
  49. unit Justone;
  50.  
  51. interface
  52.  
  53. uses
  54.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  55.   Forms, Dialogs, DsgnIntf;
  56.  
  57. type
  58.     TMyDataType = record
  59.     Name : string;
  60. end;
  61.  
  62. type
  63.   TJustOne = class(TComponent)
  64.   private
  65.     FAbout:    string;
  66.   public
  67.     constructor Create(AOwner:TComponent); override;
  68.     destructor Destroy; override;
  69.     procedure GoToPreviousInstance;
  70.     procedure ShowAbout;
  71.   published
  72.     property About: string read FAbout write FAbout stored False;
  73.     end;
  74.  
  75. procedure Register;
  76.  
  77. type
  78.   PHWND = ^HWND;
  79.   function EnumFunc(Wnd:HWND; TargetWindow:PHWND): boolean; export;
  80.  
  81. implementation
  82.  
  83. {########################################################################}
  84. type
  85.   TAboutProperty = class(TPropertyEditor)
  86.   public
  87.     procedure Edit; override;
  88.     function GetAttributes: TPropertyAttributes; override;
  89.     function GetValue:string; override;
  90.   end;
  91.  
  92. {########################################################################}
  93. procedure TAboutProperty.Edit;
  94. {Invoke the about dialog when clicking on ... in the Object Inspector}
  95. begin
  96.   TJustOne(GetComponent(0)).ShowAbout;
  97. end;
  98.  
  99. {########################################################################}
  100. function TAboutProperty.GetAttributes: TPropertyAttributes;
  101. {Make settings for just displaying a string in the ABOUT property in the
  102. Object Inspector}
  103. begin
  104.   GetAttributes := [paDialog, paReadOnly];
  105. end;
  106.  
  107. {########################################################################}
  108. function TAboutProperty.GetValue: String;
  109. {Text in the Object Inspector for the ABOUT property}
  110. begin
  111.   GetValue := '(About)';
  112. end;
  113.  
  114. {########################################################################}
  115. procedure TJustOne.ShowAbout;
  116. var
  117.     msg: string;
  118. const
  119.     carriage_return = chr(13);
  120.   copyright_symbol = chr(169);
  121. begin
  122.     msg := 'JustOne  v1.1';
  123.   AppendStr(msg, carriage_return);
  124.   AppendStr(msg, 'A Freeware component');
  125.   AppendStr(msg, carriage_return);
  126.   AppendStr(msg, carriage_return);
  127.   AppendStr(msg, 'Copyright ');
  128.   AppendStr(msg, copyright_symbol);
  129.   AppendStr(msg, ' 1995, 1996 by Steven L. Keyser');
  130.   AppendStr(msg, carriage_return);
  131.   AppendStr(msg, 'e-mail 71214.3117@compuserve.com');
  132.   AppendStr(msg, carriage_return);
  133.   ShowMessage(msg);
  134. end;
  135.  
  136. {########################################################################}
  137. procedure Register;
  138. {If you want, replace 'SLicK' with whichever component page you want
  139. JustOne to show up on.}
  140. begin
  141.   RegisterComponents('SLicK', [TJustOne]);
  142.   RegisterPropertyEditor(TypeInfo(String), TJustOne, 'About',
  143.       TAboutProperty);
  144. end;
  145.  
  146. {########################################################################}
  147. function EnumFunc(Wnd:HWND; TargetWindow:PHWND): boolean;
  148. var
  149.   ClassName : array[0..30] of char;
  150. begin
  151.   result := TRUE;
  152.   if GetWindowWord(Wnd,GWW_HINSTANCE) = hPrevInst then
  153.      begin
  154.        GetClassName(Wnd,ClassName,30);
  155.        if StrIComp(ClassName,'TApplication') = 0 then
  156.          begin
  157.            TargetWindow^ := Wnd;
  158.            result := FALSE;
  159.          end;
  160.      end;
  161. end;
  162.  
  163. {########################################################################}
  164. procedure TJustOne.GotoPreviousInstance;
  165. var
  166.   PrevInstWnd : HWND;
  167. begin
  168.   PrevInstWnd := 0;
  169.   EnumWindows(@EnumFunc,longint(@PrevInstWnd));
  170.  
  171.   if PrevInstWnd <> 0 then
  172.        if IsIconic(PrevInstWnd) then
  173.       ShowWindow(PrevInstWnd,SW_RESTORE)
  174.        else
  175.       BringWindowToTop(PrevInstWnd);
  176. end;
  177.  
  178. {########################################################################}
  179. constructor TJustOne.Create(AOwner:TComponent);
  180. begin
  181.     inherited Create(AOwner);
  182.     if hPrevInst <> 0 then
  183.     begin
  184.       GotoPreviousInstance;
  185.       halt;
  186.     end;
  187. end;
  188.  
  189. {########################################################################}
  190. destructor TJustOne.Destroy;
  191. begin
  192.   inherited Destroy;
  193. end;
  194.  
  195. {########################################################################}
  196. end.
  197.